package com.qingmeng.outsourcing.portal.controller;

import com.qingmeng.outsourcing.common.api.CommonResult;
import com.qingmeng.outsourcing.portal.enums.RegisterResult;
import com.qingmeng.outsourcing.portal.enums.SMSResult;
import com.qingmeng.outsourcing.portal.model.dto.UserRegisterParam;
import com.qingmeng.outsourcing.portal.service.UserService;
import com.sun.xml.internal.stream.StaxErrorReporter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(value = "UserController" , description = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("发送手机验证码")
    @PostMapping("/sendMessageCode")
    public CommonResult<Integer> sendMessageCode(
           @ApiParam("手机号") @RequestParam("phone") String phone,
           @ApiParam("身份表示：0-修改手机号，1-注册雇主，2-注册开发者，3-忘记密码")
           @RequestParam("identity") Integer identity){
        SMSResult smsResult = userService.sendMessageCode(phone, identity);
        if(smsResult == SMSResult.SEND_SMS_SUCCESS){
            return CommonResult.success(1, smsResult.getMessage());
        }
        return CommonResult.failed(smsResult.getMessage());
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public CommonResult<Integer> register(@RequestBody UserRegisterParam param){
        RegisterResult register = userService.register(param);
        if(register == RegisterResult.REG_SUCCESS){
            return CommonResult.success(1, register.getMessage());
        }
        return CommonResult.failed(register.getMessage());
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public CommonResult<Map<String, String>> login(
            @ApiParam("用户名") @RequestParam("username") String username,
            @ApiParam("密码") @RequestParam("password") String password){
        Map<String,String> login = userService.login(username,password);
        if(!CollectionUtils.isEmpty(login)){
            return CommonResult.success(login,"登陆成功");
        }
        return CommonResult.failed("登陆失败");
    }

    @GetMapping("/testLogin")
    public String testLogin(){
        return "你看到这个字符串，说明你已经登陆成功";
    }
}
